package logic

import (
	"context"
	"fmt"
	"zhihu-new/models/mysql"

	"zhihu-new/zhihurpc/internal/svc"
	"zhihu-new/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowProblemLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowProblemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowProblemLogic {
	return &ShowProblemLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowProblemLogic) ShowProblem(in *zhihurpc.ShowProblemRequest) (*zhihurpc.ShowProblemResponse, error) {
	// todo: add your logic here and delete this line

	var problem mysql.Problem
	err := mysql.DB.Debug().Where("id = ?", in.Id).Find(&problem).Error
	if err != nil {
		return nil, fmt.Errorf("查看详情失败")
	}

	problem.Heat = problem.Heat + 1

	mysql.DB.Debug().Updates(&problem)

	return &zhihurpc.ShowProblemResponse{
		Id:      int64(problem.ID),
		Title:   problem.Title,
		Context: problem.Context,
		Name:    problem.User.Nickname,
		Heat:    int64(problem.Heat),
	}, nil
}
